package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import com.leyou.item.vto.CategoryVTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * 分类控制器
 */
@RestController
//@CrossOrigin //跨域注解
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父id查询分类
     */
    @GetMapping("/category/of/parent")
    public ResponseEntity<List<Category>> findCategoriesById(@RequestParam("pid") Long pid){
        List<Category> categories = categoryService.findCategoriesById(pid);
        //return ResponseEntity.status(HttpStatus.OK).body(categories);
        return ResponseEntity.ok(categories);
    }

    /**
     * 根据分类ID集合 查询 分类对象集合（批量查询）
     */
    @GetMapping("/category/list")
    public ResponseEntity<List<Category>> findCategoriesByIds(@RequestParam("ids") List<Long> ids){
        List<Category> categories = categoryService.findCategoriesByIds(ids);
        return ResponseEntity.ok(categories);
    }

    /**
     * 返三级信息给前端
     */
    @GetMapping("/category/all")
    public ResponseEntity<List<CategoryVTO>> findCategories(){
        List<CategoryVTO> categorys=  categoryService.findCategories();
        return ResponseEntity.ok(categorys);
    }

}
